package com.bosc.modeling.controller;

import com.bosc.modeling.common.BaseController;
import com.bosc.modeling.common.ResponseFactory;
import com.bosc.modeling.pojo.gen.CompetitionUserEntity;
import com.bosc.modeling.pojo.gen.EnrollEntity;
import com.bosc.modeling.pojo.gen.TeamEntity;
import com.bosc.modeling.service.EnrollService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author yuanyj
 * @version 1.0
 * @date 2019/11/22 10:30
 * @description
 */
@RestController
public class EnrollController extends BaseController {
    @Autowired
    EnrollService enrollService;

    @RequestMapping(value = "/create_team",method = RequestMethod.POST)
    public Object createTeam(@RequestBody String body) throws JSONException {
        try {
            JSONObject obj = new JSONObject(body);
            String teamName = obj.getString("team_name");
            String competitionId = obj.getString("competition_id");
            String userId = obj.getString("user_id");
            String userName = obj.getString("user_name");
            boolean hasEnrolled = enrollService.hasEnrolled(competitionId,userId);
            if (hasEnrolled){
                return ResponseFactory.failureResponse("1","您已报名参赛，无法创建队伍");
            }else {
                String teamId = UUID.randomUUID().toString();
                Date date = new Date();
                TeamEntity team = new TeamEntity();
                team.setTeamId(teamId);
                team.setTeamName(teamName);
                team.setUserId(userId);
                team.setUserName(userName);
                team.setIsOwner((short) 0);
                team.setTime(date);

                EnrollEntity enroll = new EnrollEntity();
                enroll.setCompetitionId(competitionId);
                enroll.setTeamId(teamId);
                enroll.setTime(date);

                CompetitionUserEntity competitionUserEntity = new CompetitionUserEntity();
                competitionUserEntity.setCompetitionId(competitionId);
                competitionUserEntity.setUserId(userId);
                competitionUserEntity.setTime(date);

                enrollService.insertTeam(team);
                enrollService.insertEnroll(enroll);
                enrollService.insertCompUser(competitionUserEntity);
                return ResponseFactory.successResponse(teamId);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @RequestMapping(value = "/check_enroll",method = RequestMethod.POST)
    public Object checkEnroll(@RequestBody String body) throws JSONException {
        try {
            JSONObject obj = new JSONObject(body);
            String competitionId = obj.getString("competition_id");
            String userId = obj.getString("user_id");
            boolean hasEnrolled = enrollService.hasEnrolled(competitionId,userId);
            if (!hasEnrolled){
                return ResponseFactory.failureResponse(null,null);
            }else {
                return ResponseFactory.successResponse(null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
    @RequestMapping(value = "/search_team",method = RequestMethod.GET)
    public Object searchTeam(@RequestParam(value = "keywords") String key) throws JSONException {
        log.info("searchTeam:"+key);
        try {
            List<TeamEntity> teams = enrollService.getTeamByName(key);
            HashMap<String,Object> resp = new HashMap<>();
            resp.put("type","success");
            resp.put("code",0);
            resp.put("content",teams);
            return resp;
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @RequestMapping(value = "/join_team",method = RequestMethod.POST)
    public Object joinTeam(@RequestBody String body) throws JSONException {
        log.info("joinTeam:"+body);
        try {
            JSONObject obj = new JSONObject(body);
            String userId = obj.getString("user_id");
            String userName = obj.getString("user_name");
            String teamId = obj.getString("team_id");
            String teamName = obj.getString("team_name");
            String competitionId = obj.getString("competition_id");
            if (enrollService.isExceedLimit(teamId)){
                return ResponseFactory.failureResponse("2","组队人数不能超过3");
            }else {
                Date date = new Date();
                TeamEntity team = new TeamEntity();
                team.setTime(date);
                team.setIsOwner((short)1);
                team.setUserName(userName);
                team.setUserId(userId);
                team.setTeamName(teamName);
                team.setTeamId(teamId);
                CompetitionUserEntity entity = new CompetitionUserEntity();
                entity.setTime(date);
                entity.setUserId(userId);
                entity.setCompetitionId(competitionId);
                enrollService.insertTeam(team);
                enrollService.insertCompUser(entity);
                return ResponseFactory.successResponse(null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
